﻿
using Microsoft.CodeAnalysis.CSharp.Syntax;
using QRCoder;
using SixLabors.ImageSharp.PixelFormats;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ZXing;
using ZXing.Common;
namespace CommandLib
{
    /// <summary>
    /// 二维码帮助类
    /// </summary>
    public static class QrCodeHelper
    {
        /// <summary>
        /// 创建二维码
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public static Bitmap CreateQrCode(string txt)
        {

            Bitmap result = null;
            using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
            using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(txt, QRCodeGenerator.ECCLevel.Q))
            using (QRCode qrCode = new QRCode(qrCodeData))
            {
                result = qrCode.GetGraphic(20);
            }
            return result;
        }


        /// <summary>
        ///  带logo的二维码
        /// </summary>
        /// <param name="txt">内容</param>
        /// <param name="logoPath">logo路径</param>
        /// <param name="pixel">相素默认为20</param>
        /// <returns></returns>
        public static Bitmap GetLogoQRCode(string txt, string logoPath, int pixel = 20)
        {
            QRCodeGenerator generator = new QRCodeGenerator();
            QRCodeData codeData = generator.CreateQrCode(txt, QRCodeGenerator.ECCLevel.M, true);
            QRCoder.QRCode qrcode = new QRCoder.QRCode(codeData);
            Bitmap icon = Bitmap.FromFile(logoPath) as Bitmap;
            Bitmap qrImage = qrcode.GetGraphic(pixel, Color.Black, Color.White, icon, 15, 6, true);
            return qrImage;
        }
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="txt"></param>
        /// <returns></returns>
        public static Bitmap GetQRCodeEncoder(string txt)
        {
            Bitmap result = null;
            try
            {
                QRCodeEncoder endocder = new QRCodeEncoder();
                //二维码背景颜色
                endocder.QRCodeBackgroundColor = Color.White;
                //二维码编码方式
                endocder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
                //每个小方格的宽度
                endocder.QRCodeScale = 10;
                //二维码版本号
                endocder.QRCodeVersion = 5;
                //纠错等级
                endocder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;

                //将json川做成二维码
                result = endocder.Encode(txt, Encoding.UTF8);
            }
            catch (Exception)
            {
                throw;
            }
            return result;

        }

        /// <summary>
        /// 生成条码
        /// </summary>
        /// <param name="text">生成内容</param>
        /// <param name="_Width">宽度</param>
        /// <param name="_Height">高度</param>
        public static Bitmap BarCoder(string text, int _Width = 150, int _Height = 50)
        {
            Bitmap result = null;
            try
            {
                var writer = new BarcodeWriter<Bitmap>();
                //使用ITF 格式，不能被现在常用的支付宝、微信扫出来
                //如果想生成可识别的可以使用 CODE_128 格式
                //writer.Format = BarcodeFormat.ITF;
                writer.Format = BarcodeFormat.CODE_128;
                EncodingOptions options = new EncodingOptions()
                {
                    Width = _Width,
                    Height = _Height,
                    Margin = 2
                };
                writer.Options = options;
                result = writer.Write(text);
            }
            catch (Exception)
            {
                throw;
            }

            return result;

        }
        /// <summary>
        /// 二维码识别
        /// </summary>
        /// <param name="QrImgfile">图片地址</param>
        /// <returns></returns>
        public static string DeQRCoder(string QrImgfile)
        {
            string result = "";
            try
            {
                if (File.Exists(QrImgfile))
                {
                    QRCodeDecoder decoder = new QRCodeDecoder();
                    var bmp = Image.FromFile(QrImgfile) as Bitmap;
                    result = decoder.decode(new QRCodeBitmapImage(bmp), Encoding.UTF8);
                }
            }
            catch (Exception)
            {

                throw;
            }

            return result;
        }
        /// <summary>
        /// 二维码识别
        /// </summary>
        /// <param name="QrImg"></param>
        /// <returns></returns>
        public static string DeQRCoder(Bitmap QrImg)
        {
            string result = "";
            try
            {
                if (QrImg != null)
                {
                    QRCodeDecoder decoder = new QRCodeDecoder();
                    result = decoder.decode(new QRCodeBitmapImage(QrImg), Encoding.UTF8);
                }
            }
            catch (Exception)
            {
                throw;
            }

            return result;
        }



        /// <summary>
        /// 识别二维码/条形码
        /// </summary>
        /// <param name="filename">文件</param>
        /// <returns></returns>
        public static string DeBarCode(string filename)
        {
            var result = string.Empty;
            if (File.Exists(filename))
            {
                try
                {
                    var reader = new BarcodeReader<Bitmap>((souc) =>
                    {
                        return null;
                    });
                    reader.Options.CharacterSet = "UTF-8";
                    var map = Image.FromFile(filename) as Bitmap;
                    Result bt = reader.Decode(map);
                    result = bt?.Text;
                }
                catch (Exception)
                {
                    throw;
                }

            }
            return result;
        }

        /// <summary>
        /// 将image转换成byte[]数据
        /// </summary>
        /// <param name="_image"></param>
        /// <returns></returns>
        private static byte[] imageToByte(this Image _image)
        {
            using (var ms = new MemoryStream())
            {
                _image.Save(ms, ImageFormat.Jpeg);
                return ms.ToArray();
            }

        }
        /// <summary>
        /// 将byte[]数据转换成image
        /// </summary>
        /// <param name="myByte"></param>
        /// <returns></returns>
        private static Image byteToImage(this byte[] myByte)
        {
            using (var ms = new MemoryStream(myByte))
            {
                Image _Image = Image.FromStream(ms);
                return _Image;
            }
        }

    }
}
